package com.haochang.chunkvod.app.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

/**
 *     @author Yangjie
 *     className JsonParseUtils
 *     created at  2016/9/3  12:00
 *     json解析类
 */
public class JsonParseUtils
{

	public static String optString(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			return object.optString(key, "");
		} catch (JSONException e)
		{
			e.printStackTrace();
			return "";
		}
	}

	public static String optString(JSONObject jsonObject, String key)
	{
		return optString(jsonObject, key, "");
	}

	public static String optString(JSONObject jsonObject, String key, String defaultValue)
	{
		try
		{
			String optString = jsonObject.optString(key, defaultValue);
			if (TextUtils.isEmpty(optString))
			{
				return defaultValue;
			}
			return optString;
		} catch (Exception e)
		{
			e.printStackTrace();
			return defaultValue;
		}
	}

	public static boolean optBoolean(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			return object.optBoolean(key, false);
		} catch (JSONException e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public static boolean optBoolean(JSONObject jsonObject, String key)
	{
		try
		{
			return jsonObject.optBoolean(key, false);
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public static int optInt(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			return object.optInt(key, 0);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Integer.MAX_VALUE;
		}
	}

	public static int optInt(JSONObject jsonObject, String key)
	{
		try
		{
			return jsonObject.optInt(key, 0);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Integer.MAX_VALUE;
		}
	}

	public static float optFloat(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			String optString = object.optString(key, "");
			return Float.parseFloat(optString);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Float.MAX_VALUE;
		}
	}

	public static float optFloat(JSONObject jsonObject, String key)
	{
		try
		{
			String optString = jsonObject.optString(key, "");
			return Float.parseFloat(optString);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Float.MAX_VALUE;
		}
	}

	public static double optDouble(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			String optString = object.optString(key, "");
			return Double.parseDouble(optString);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Double.MAX_VALUE;
		}
	}

	public static double optDouble(JSONObject jsonObject, String key)
	{
		try
		{
			String optString = jsonObject.optString(key, "");
			return Double.parseDouble(optString);
		} catch (Exception e)
		{
			e.printStackTrace();
			return Double.MAX_VALUE;
		}
	}

	public static JSONObject optJSONObject(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			return object.optJSONObject(key);
		} catch (JSONException e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static JSONObject optJSONObject(JSONObject jsonObject, String key)
	{
		try
		{
			return jsonObject.optJSONObject(key);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static JSONArray optJSONArray(String response, String key)
	{
		try
		{
			JSONObject object = new JSONObject(response);
			return object.optJSONArray(key);
		} catch (JSONException e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static JSONArray optJSONArray(JSONObject jsonObject, String key)
	{
		try
		{
			return jsonObject.optJSONArray(key);
		} catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
}
